//# sourceURL=companyDetail.js
layui.element.on('tab(login-user-tabs)', function (layui_obj) {

    var companyId;
    var companyDetail_fn = {
        initRender: function () {
            //设置动态tab页内容主体的高度
            cmsUtils.setContentTabHeight('companyDetailContent', 50);
            $('#companyDetail').attr('hasRender', true);
            var url = '/inviteCompany/currentDetail';
            cmsUtils.renderDynamicSelectionBox('/cms/common/combo/provinceList', 'companyDetail-provenceCode', 'provinceCode', 'provinceName');
            $.get(url, function (data) {
                console.log(data);
                companyDetail_fn.setFormData(data);
                $('#companyDetail-div-createDate').show();
                $('#companyDetail-input-companyName').attr('lay-verify', 'required');
                $('#companyDetail-input-companyName').attr('lay-verType', 'tips');
                $('#companyDetail-input-industry').attr('lay-verify', 'required');
                $('#companyDetail-input-industry').attr('lay-verType', 'tips');
                var pic = [];
                pic.push(data.data.imgUrl);
                picUtils.initPicBox('companyDetail-img-id', pic, false);
                companyId = data.data.id;
            });
            layui.form.render(null, 'companyDetail-form');
        },
        setFormData: function (data) {
            if (data.data.provinceCode) {
                cmsUtils.renderDynamicSelectionBox('/cms/common/combo/cityList?provinceCode=' + data.data.provinceCode, 'companyDetail-cityCode', 'cityCode', 'cityName');
            }
            cmsUtils.renderDynamicSelectionBox('/cms/common/combo/result/financing','companyDetail-financing','code','value');
            cmsUtils.renderDynamicSelectionBox('/cms/common/combo/result/scale','companyDetail-scale','code','value');
            cmsUtils.formDataBind('companyDetail-form', data.data);
            if (data.data.lng && data.data.lat) {
                var map = new AMap.Map('companyDetail-container', {
                    resizeEnable: true,
                    zoom: 13,
                    center: [data.data.lng, data.data.lat]
                });
                AMap.plugin('AMap.Geocoder', function () {//回调函数
                    var geocoder = new AMap.Geocoder({
                        city: data.data.cityCode
                    });
                    var input = document.getElementById('companyDetail-address');
                    var marker = new AMap.Marker({
                        map: map,
                        bubble: true
                    });
                    map.on('click', function (e) {
                        $('#companyDetail-lng').val(e.lnglat.lng);
                        $('#companyDetail-lat').val(e.lnglat.lat);
                        marker.setPosition(e.lnglat);
                        geocoder.getAddress(e.lnglat, function (status, result) {
                            if (status == 'complete') {
                                input.value = result.regeocode.formattedAddress
                            } else {

                            }
                        })
                    });

                    input.onchange = function (e) {
                        var address = input.value;
                        geocoder.getLocation(address, function (status, result) {
                            if (status == 'complete' && result.geocodes.length) {
                                var lnglat = result.geocodes[0].location;
                                $('#dealerUserDetail-lng').val(lnglat.lng);
                                $('#dealerUserDetail-lat').val(lnglat.lat);
                                marker.setPosition(result.geocodes[0].location);
                                map.setCenter(marker.getPosition())
                            } else {
                            }
                        })
                    };
                })
            }
            layui.form.render(null, 'companyDetail-form');
        },
        getFormData: function (data) {
            data.id = companyId;
            data.imgUrl = $('#companyDetail-img-id').find('img')[0].src;
            return data;
        },
        verifyForm: function () {
            var bool = formVerifyUtils.picFieldChecker('companyDetail-btn-id','companyDetail-img-id');
            return bool;
        },
        submitForm: function (post_params, action, index) {
            console.log(post_params);
            $.ajax({
                type: 'post',
                url: '/inviteCompany/saveOrUpdate',
                data: post_params,
                dataType: 'json',
                success: function (data) {
                    layer.close(index);
                    if (data.code == 200) {
                        layer.msg('保存成功', {anim: 6});
                    } else {
                        layer.msg(data.msg, {anim: 6});
                    }
                }
            });
        }
    }
    if ($('#companyDetail')[0] && !$('#companyDetail').attr('hasRender')) {
        companyDetail_fn.initRender();
        // 设置页面高度
        $(window).resize(function() {
            cmsUtils.setContentTabHeight('companyDetailContent', 50);
        });

        //logo添加
        var option = {
            btn_id: 'companyDetail-btn-id',
            ul_id: 'companyDetail-img-id',
            max_file_count: 1
        }
        picUtils.uploadInit(0, option);
    }
    // 修改
    layui.form.on('submit(companyDetail-btn-modify)', function (data) {
        console.log(34234);
        if (!companyDetail_fn.verifyForm()) {
            console.log(data);
            return;
        }
        var msg = '确定保存修改信息？';
        layer.confirm(msg, {
            btn:['确定', '取消']
        }, function (index) {
            companyDetail_fn.submitForm(companyDetail_fn.getFormData(data.field), 'close', index);
        });
    });


    //根据省份级联加载对应城市
    layui.form.on('select(companyDetail-provenceCode)', function(data){
        $("#companyDetail-cityCode").empty();
        $("#companyDetail-cityCode").append("<option value=''>"+"选择城市"+"</option>");
        if (data.value){
            cmsUtils.renderDynamicSelectionBox('/cms/common/combo/cityList?provinceCode='+data.value,'companyDetail-cityCode','cityCode','cityName');
        }else {
            layui.form.render('select');
        }
    });
    layui.form.on('select(companyDetail-cityCode)', function(data){
        console.log(data);
        AMap.plugin('AMap.Geocoder',function(){//回调函数
            //实例化Geocoder
            var cityName = $("#companyDetail-cityCode").find("option:selected").text();
            var cityCode = $("#companyDetail-cityCode").find("option:selected").val();
            var geocoder = new AMap.Geocoder({
                city: cityCode
            });
            var map = null;
            geocoder.getLocation(cityName, function(status, result) {
                console.log(status);
                console.log(result);
                if (status === 'complete' && result.info === 'OK') {
                    if (result.geocodes.length > 0) {
                        map = new AMap.Map('companyDetail-container', {
                            resizeEnable: true,
                            zoom:11,
                            center: [result.geocodes[0].location.lng, result.geocodes[0].location.lat]
                        });
                        var marker = new AMap.Marker({
                            map:map,
                            bubble:true
                        })
                        var input = document.getElementById('companyDetail-address');
                        map.on('click',function(e){
                            $('#companyDetail-lng').val(e.lnglat.lng);
                            $('#companyDetail-lat').val(e.lnglat.lat);
                            marker.setPosition(e.lnglat);
                            geocoder.getAddress(e.lnglat,function(status,result){
                                if(status=='complete'){
                                    input.value = result.regeocode.formattedAddress
                                }else{
                                    alert("获取地址经纬度失败");
                                }
                            })
                        })

                        input.onchange = function(e){
                            var address = input.value;
                            geocoder.getLocation(address,function(status,result){
                                if(status=='complete'&&result.geocodes.length){
                                    var lnglat = result.geocodes[0].location;
                                    $('#companyDetail-lng').val(lnglat.lng);
                                    $('#companyDetail-lat').val(lnglat.lat);
                                    marker.setPosition(result.geocodes[0].location);
                                    map.setCenter(marker.getPosition())
                                }else{
                                    alert("获取地址经纬度失败");
                                }
                            })
                        }
                    }
                }else{
                    //获取经纬度失败
                    alert("获取地址经纬度失败");
                }
            });
        })
    });
});